﻿using MediatR;
using U.B.DoMain;
using U.B.ErrorCode;
using U.B.ReadApi.Command;
using U.B.Repistory;

namespace U.B.ReadApi.CommandHander
{
    public class GetLeaveCommandHander : IRequestHandler<GetLeaveCommand, Apipage<LeaveApp>>
    {
        private readonly IRepistory<LeaveApp> leaveRep;
        private readonly IRepistory<User> userRep;
        private readonly IRepistory<Approval> approvalRep;
         public GetLeaveCommandHander(IRepistory<LeaveApp> leaveRep, IRepistory<User> userRep, IRepistory<Approval> approvalRep)
        {
            this.leaveRep = leaveRep;
            this.userRep = userRep;
            this.approvalRep = approvalRep;
        }
        public Task<Apipage<LeaveApp>> Handle(GetLeaveCommand request, CancellationToken cancellationToken)
        {
            Apipage<LeaveApp> result = new Apipage<LeaveApp>();
            result.code = Renum.显示成功;
            result.Message = "显示成功";
            var leave = leaveRep.GetAll().Where(x => x.IsDeleted==false).ToList();
            //查询当前登录人是否是请假申请的申请人且是员工
            List<LeaveApp>? m = leave.Where(x => x.LeaveId == request.UserId && x.PowerId == (int)PowerId.员工).ToList();
            if (m.Count() > 0)
            {
                //是 请假申请的申请人且是员工
                if (!string.IsNullOrEmpty(request.LeaveName))
                {
                    m = m.Where(x => x.LeaveName.Contains(request.LeaveName)).ToList();

                }
                if (!string.IsNullOrEmpty(request.StartTime))
                {
                    m = m.Where(x => x.StartTime >= DateTime.Parse(request.StartTime)).ToList();
                }
                if (!string.IsNullOrEmpty(request.EndTime))
                {
                    m = m.Where(x => x.EndTime < DateTime.Parse(request.EndTime).AddDays(1)).ToList();
                }
                if (request.StatusId > 0)
                {
                    m = m.Where(x => x.StatusId == request.StatusId).ToList();
                }
                result.TotalCount = m.Count();
                result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                result.Items = m.OrderByDescending(x => x.LAId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                return Task.FromResult(result);
            }

            var u = userRep.GetAll().Where(x => x.DepartmentId == request.DepartmentId && x.PowerId == (int)PowerId.部门领导人);


            //不是
            //查询登录人是否是领导
            if (u.Count() > 0)
            {
                //是
                var le = leave.Where(x => x.DepartmentId == request.DepartmentId && x.PowerId == (int)PowerId.员工 || x.LeaveId == request.UserId);
                if (!string.IsNullOrEmpty(request.LeaveName))
                {
                    le = le.Where(x => x.LeaveName.Contains(request.LeaveName));
                }

                if (!string.IsNullOrEmpty(request.StartTime))
                {
                    le = le.Where(x => x.StartTime >= DateTime.Parse(request.StartTime));
                }

                if (!string.IsNullOrEmpty(request.EndTime))
                {
                    le = le.Where(x => x.EndTime < DateTime.Parse(request.EndTime).AddDays(1));
                }
                if (request.StatusId > 0)
                {
                    le = le.Where(x => x.StatusId == request.StatusId);
                }
                result.TotalCount = le.Count();
                result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                result.Items = le.OrderByDescending(x => x.LAId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

                return Task.FromResult(result);
            }


            //查询当前登录人是否是总负责人
            var z = userRep.GetAll().Where(x => x.UserId == request.UserId && x.PowerId == (int)PowerId.总负责人).ToList();
            if (z.Count() > 0)
            {
                leave = leave.Where(x => x.StatusId == (int)Status.已审批 || x.StatusId == (int)Status.终审_已通过 || x.StatusId == (int)Status.终审_已拒绝 || x.PowerId == (int)PowerId.部门领导人).ToList();
                if (!string.IsNullOrEmpty(request.LeaveName))
                {
                    leave = leave.Where(x => x.LeaveName.Contains(request.LeaveName)).ToList();

                }
                if (!string.IsNullOrEmpty(request.StartTime))
                {
                    leave = leave.Where(x => x.StartTime >= DateTime.Parse(request.StartTime)).ToList();
                }
                if (!string.IsNullOrEmpty(request.EndTime))
                {
                    leave = leave.Where(x => x.EndTime < DateTime.Parse(request.EndTime).AddDays(1)).ToList();
                }
                if (request.StatusId > 0)
                {
                    leave = leave.Where(x => x.StatusId == request.StatusId).ToList();
                }
                result.TotalCount = leave.Count();
                result.PageCount = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
                result.Items = leave.OrderByDescending(x => x.LAId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            }


            return Task.FromResult(result);
        }
    }
}
